package com.dr.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dr.common.ValueUtil;
import com.dr.pojo.Dept;
import com.dr.pojo.Invoice;
import com.dr.pojo.Regist;
import com.dr.pojo.RegistLst;
import com.dr.pojo.Scheduling;
import com.dr.service.DeptService;
import com.dr.service.InvoiceService;
import com.dr.service.RegistLstService;
import com.dr.service.RegistService;
import com.dr.service.SchedulingService;

@Controller
@RequestMapping("/rg")
public class RegistController {
	@Autowired
	private InvoiceService invoiceService; //Spring注入ServiceImpl的实例

	@Autowired
	private RegistService registService; 
	@Autowired
	private DeptService deptService; 
	@Autowired
	private RegistLstService registLstService; 
	@Autowired
	private SchedulingService schedulingService; 

	/*
	 * 加载挂号页面
	 */
	@RequestMapping("/regist")
	public String regist(Model md,RegistLst registLst) {
		
//invoice		
		//发票号
		Invoice invoice = invoiceService.nextInvoice();
		if(invoice != null){
			md.addAttribute("invoice", invoice);
		}else {
			md.addAttribute("invoice", new Invoice());
			md.addAttribute("errInvoice", "发票已用完，请重新申请");
		}
		//病历号
		String caseNumber = registLstService.nextCaseNumber();
		if(registLst.getCaseNumber()==null) {
			md.addAttribute("caseNumber", caseNumber);
		}else {
			md.addAttribute("caseNumber", registLst.getCaseNumber());
		}
		//挂号级别
		List<Regist> registList = registService.findRegistList();
		List<Dept> deptList = deptService.findDeptList();
		if( registList.size() != 0){
			md.addAttribute("registList", registList);
		}else {
			md.addAttribute("registList", new ArrayList<>());
			md.addAttribute("errRegist", "获取挂号级别信息失败！");
		}
		md.addAttribute(deptList);
		return "regist/regist.html";
	}
	
	/*
	 * 
	 * 查询病历信息
	 */
	@RequestMapping("/caseInfo")
	public String caseInfo(Model md,String caseNumber) {
		if(ValueUtil.isEmpty(caseNumber)) {
			caseNumber = "";
		}
		
		//根据病历号，查询病历信息
		RegistLst registLst = registLstService.findRegistByCaseNumber(caseNumber);
		
		if(registLst == null) {
			registLst = new RegistLst();
		} else {
			registLst.setRegistLeID(0);  //查询病历信息时，不需要保持查询医生用的挂号级别
			registLst.setDeptName("");
		}
		
		md.addAttribute("registLst", registLst);
		return regist(md,registLst);
	}
	
	/*
	 * 查询医生信息
	 * 
	 * */
	
	@RequestMapping("/schedulingInfo")
	public String schedulingInfo(Model md,RegistLst registLst) {
		List<Scheduling> schedulingList= schedulingService.findSchedulingList(registLst);
		md.addAttribute("schedulingList", schedulingList);
		//根据病历号，查询病历信息
		return regist(md,registLst==null?new RegistLst():registLst);
	}
}



